/**
 * 
 */
package com.ymatou.api.sellerpromotion.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.hamcrest.core.IsEqual;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.api.marketingseller.service.CreateSellerPromotionCall;
import com.ymatou.api.sellerpromotion.service.GetPromotionBySellerIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.SellerPromotionCallService;
import com.ymttest.database.mongo.SellerPromotionInfoWapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Ts_GetPromotionBySellerId {
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取买手当前有效的促销活动");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetPromotionBySellerIdCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionBySellerId_001() {
		Logger.start(true, "买手存在进行中的满减促销，查询结果与mongo 一致");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			SellerPromotionCallService.Create3Promotions();
			
			// 促销商品ID
			// 获取mongo 数据
			ArrayList<Map> mongoMap = new SellerPromotionInfoWapper().getPromotions(sellerId);
			boolean IsShowProducts = true;
			JSONObject GetPromotionBySellerIdBean = new GetPromotionBySellerIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"IsShowProducts",IsShowProducts));
			
			Logger.verifyEquals(200,GetPromotionBySellerIdBean.getInt("Code"), "响应报文-返回码");
			// 校验mongo 数据库数据
			JSONArray PromotionInfoBean = GetPromotionBySellerIdBean.getJSONObject("Data").getJSONArray("PromotionPolicys");
			for(int i=0;i<PromotionInfoBean.length();i++){
				JSONObject sellerpromotionsinf = (JSONObject) PromotionInfoBean.get(i);
				Map<String,Object> mongoDataMap=  mongoMap.get(i);
				Date a = (Date)mongoDataMap.get("PromotionBeginTime");
				Date b = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextDay(0));
				if(mongoDataMap.get("PromotionId").equals(sellerpromotionsinf.getInt("PromotionId"))){
						
					Logger.verifyEquals(sellerpromotionsinf.getInt("SellerId"), mongoDataMap.get("SellerId"), "买手ID");
					Logger.verifyEquals(sellerpromotionsinf.getString("PromotionName"), mongoDataMap.get("PromotionName"), "满减促销名称");
					Logger.verifyEquals(sellerpromotionsinf.getInt("PromotionType"), mongoDataMap.get("PromotionType"), "满减促销类型");
					Logger.verifyEquals(sellerpromotionsinf.getString("PromotionDesc"), mongoDataMap.get("PromotionDesc"), "满减促销描述");
					AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(YMTDateUtil.parseRespDate2(sellerpromotionsinf.getString("PromotionBeginTime"))), (Date)mongoDataMap.get("PromotionBeginTime"), 60);
					AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(YMTDateUtil.parseRespDate2(sellerpromotionsinf.getString("PromotionEndTime"))), (Date)mongoDataMap.get("PromotionEndTime"), 60);
					Logger.verifyEquals(sellerpromotionsinf.getInt("PromotionLimitNum"), mongoDataMap.get("PromotionLimitNum"), "满减促销买家限购数");
					Logger.verifyEquals(sellerpromotionsinf.getBoolean("CanUseSellerCoupon"), mongoDataMap.get("CanUseSellerCoupon"), "满减促销是否使用买手优惠券");
					Logger.verifyEquals(sellerpromotionsinf.getBoolean("IsSameProductUse"), mongoDataMap.get("IsSameProductUse"), "是否同商品满减");
					Logger.verifyEquals(sellerpromotionsinf.getBoolean("IsSpecifyProduct"), mongoDataMap.get("IsSpecifyProduct"), "是否指定商品活动");
					Logger.verifyEquals(sellerpromotionsinf.getBoolean("IsLimitProduct"), mongoDataMap.get("IsLimitProduct"), "是否为排除商品活动");	
					Logger.verifyEquals(sellerpromotionsinf.getJSONArray("PromotionValues").getJSONObject(0).getInt("QtyCondition"), new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getInt("QtyCondition"), "满减促销件数");
					Logger.verifyEquals(Float.valueOf(sellerpromotionsinf.getJSONArray("PromotionValues").getJSONObject(0).getString("Value")), Float.valueOf(new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getString("Value")), "优惠折扣");
					Logger.verifyEquals(Float.valueOf(sellerpromotionsinf.getJSONArray("PromotionValues").getJSONObject(0).getInt("AmountCondition")), Float.valueOf(new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getString("AmountCondition")), "优惠折扣");
					
					JSONArray prodSqlArray = new JSONArray();
					for(int j = 0;j<sellerpromotionsinf.getJSONArray("PromotionProducts").length();j++){
						prodSqlArray.put(sellerpromotionsinf.getJSONArray("PromotionProducts").getJSONObject(j).get("ProductId"));
					}
					JSONArray productIds = new JSONArray(mongoDataMap.get("ProductIds").toString());
					AssertService.checkArray(productIds,prodSqlArray);
				} else if(!mongoDataMap.get("PromotionId").equals(sellerpromotionsinf.getInt("PromotionId")) && a.after(b)) {
					System.out.println(mongoDataMap.get("PromotionId"));
				}			
			} 
			
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionBySellerId_002() {
		Logger.start(true, "买手不存在正在进行中的促销，查询结果为空");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			// 促销商品ID
			// 获取mongo 数据
			ArrayList<Map> mongoMap = new SellerPromotionInfoWapper().getPromotions(sellerId);
			//Logger.verifyIsNull(, Description)
			boolean IsShowProducts = true;
			JSONObject GetPromotionBySellerIdBean = new GetPromotionBySellerIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"IsShowProducts",IsShowProducts));
			
			Logger.verifyEquals(200,GetPromotionBySellerIdBean.getInt("Code"), "响应报文-返回码");
			// 校验mongo 数据库数据
			Logger.verifyIsNull(GetPromotionBySellerIdBean.getJSONObject("Data").getString("PromotionPolicys"),"促销策略不存在");
			 
			
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


